package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.mainframeSub.configfile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;

public class ConfigFileHandler {

	private static final String DEFAULT_CONFIGFILEPATH = System
			.getProperty("user.home")+"\\CMS";
	private static final String DEFAULT_CONFIGFILENAME = "\\cms_configfile.cfg";

	public String getLaFClass() {

		File configfile = new File(DEFAULT_CONFIGFILEPATH
				+ DEFAULT_CONFIGFILENAME);

		
		if (configfile != null) {

			if (configfile.isFile() && configfile.canRead()) {
				return readirstLine(configfile);
			}
		}
		new File(DEFAULT_CONFIGFILEPATH).mkdir();
		return "";
	}
	
	public void writeFirstLine(String laf) {
		
		try {
			PrintWriter printWriter = new PrintWriter(new File(DEFAULT_CONFIGFILEPATH, DEFAULT_CONFIGFILENAME));
			printWriter.println(laf);
			printWriter.close();
		} catch (FileNotFoundException e) {

		}
		
	}
	

	private String readirstLine(File configfile) {

		String laf = "";

		try {
			FileReader fileReader = new FileReader(configfile);
			Scanner scanner = new Scanner(fileReader);
			laf = scanner.next();
		} catch (FileNotFoundException e) {

			return "";
		}

		return laf;
	}
	
	
}
